import {
	mapGetters,
	mapMutations
} from 'vuex'
import wechath5 from '@/utils/wechath5'
import {
	postPosition
} from '../api/position'
import {
	isWeixinClient,
	currentPage,
	paramsToStr,
	acountList
} from '@/utils/tools'
import Cache from "@/utils/cache"
import {
	inputInviteCode
} from "@/api/user"
import {
	BACK_URL
} from '@/config/cachekey'
import QQMapWx from '@/utils/qqmap-wx-jssdk.min.js'
import {
	imgUrl
} from '../config/app'
export default {
	data() {
		return {
			primaryColor: "#24A3FF",
			QQMAPSDK: null,
			imgUrl: imgUrl
		};
	},
	async onLoad(option) {
		this.QQMAPSDK = new QQMapWx({
			key: this.$store.state.app.mapKey
		})
		// #ifdef H5
		const route = currentPage().route
		// 记录登录后要返回的路径
		if (!acountList.includes(route) && !this.isLogin) {
			Cache.set(BACK_URL, `/${route}${paramsToStr(option)}`)
		}
		if (isWeixinClient()) {
			// jssdk配置
			await wechath5.config()
			if ((['package_goods/pages/goods_detail/index', "pages/bundle/bargain_process/bargain_process"]
					.includes(
						route))) {
				return;
			}
			setTimeout(() => {
				// 分享配置
				this.wxShare()
			}, 500)
		}
		// #endif

	},
	methods: {
		/**
		 * 获取地理位置
		 */
		getLocation(callback = null) {
			let resData = null;
			const _this = this;
			uni.getLocation({
				type: 'gcj02',
				geocode: true,
				isHighAccuracy: true,
				accuracy: 'best',
				cacheTimeout: 24 * 60 * 60,
				success(res) {
					uni.setStorage({
						key: 'City_lat',
						data: res.latitude,
					})
					uni.setStorage({
						key: 'City_lng',
						data: res.longitude,
					})
					resData = {
						lat: res.latitude,
						lng: res.longitude,
					}
					console.log('location', res)
					postPosition({
						lat: res.latitude,
						lng: res.longitude
					}).then(ress => {
						if (ress.code != 1) return;
						console.log('qqmap>>', ress)
						let CityName = ress.data.name?.city?.city_name || '';
						let DistrictName = ress.data.name?.county?.city_name || '';
						let StreetName = ress.data.name?.township?.city_name || '';
						let VillageName = ress.data.name?.village?.city_name || '';
						resData.CityName = CityName + DistrictName + StreetName + VillageName;
						resData.address = ress.data.name;
						resData.city = CityName;
						uni.setStorage({
							key: 'City_Name',
							data: resData.CityName
						})
						if (callback) callback(resData);
					})
					// _this.QQMAPSDK.reverseGeocoder({
					// 	location: {
					// 		latitude: res.latitude,
					// 		longitude: res.longitude
					// 	},
					// 	success(ress) {
					// 		console.log('qqmap>>', ress)
					// 		let CityName = ress.result.address_component.city;
					// 		let DistrictName = ress.result.address_component.district;
					// 		let StreetName = ress.result.address_component.street;
					// 		resData.CityName = CityName + DistrictName + StreetName;
					// 		resData.address = ress.result.address_component;
					// 		uni.setStorage({
					// 			key: 'City_Name',
					// 			data: resData.CityName
					// 		})
					// 		if (callback) callback(resData);
					// 	},
					// 	fail(error) {
					// 		console.dir(error)
					// 		uni.showToast({
					// 			'title': '对不起，数据获取失败！',
					// 			'icon': 'none'
					// 		})
					// 	}
					// })
				},
				fail(res) {
					uni.showToast({
						'title': '对不起，获取位置失败！',
						'icon': 'none'
					})
					console.log('error', res)
				}
			})
		},
		wxShare(options) {
			const shareInfo = Cache.get("shareInfo")
			const sym = window.location.href.includes('?') ? '&' : '?'
			const option = {
				shareTitle: "100%开源免费商用电商系统",
				shareLink: window.location.href + sym + "invite_code=" + this.$store.getters.inviteCode,
				shareImage: "",
				shareDesc: "likeshop已经把小程序、安卓APP、苹果APP都免费开源出来了"
			}
			if (!options) {
				if (shareInfo) {
					option.shareImage = shareInfo.h5_share_image
					option.shareTitle = shareInfo.h5_share_title
					option.shareDesc = shareInfo.h5_share_intro
				}
				wechath5.share(option)
			} else {
				wechath5.share(options)
			}
		}
	},
	computed: {
		...mapGetters(['isLogin']),
	},

	// 全局配置分享
	onShareAppMessage() {
		const inviteCode = this.$store.getters.inviteCode
		const shareInfo = Cache.get("shareInfo")
		console.log(shareInfo)
		const share = {
			title: shareInfo.mnp_share_title,
			path: `/pages/index/index?invite_code=${inviteCode}`,
			imageUrl: shareInfo.mnp_share_bg,
		}
		return share
	},
	//  分享朋友圈
	onShareTimeline() {
		const shareInfo = Cache.get("shareInfo")
		console.log(shareInfo)
		const share = {
			title: shareInfo.mnp_share_title,
			imageUrl: shareInfo.mnp_share_bg
		}
		return share
	}

};